package org.kineticsproject.visualizations;

import java.io.IOException;
import java.text.SimpleDateFormat;

public class KMLWriterTimeStamp extends KMLWriter {

	
	private int 	icrumbCounter;
	final int 		CRUMBSPERLOOK = 5;
	
	public KMLWriterTimeStamp()
	{	
		super();
		icrumbCounter = 0;
		
	}
	

	

	
	  public boolean process()
	  {
		  boolean result = true;
		  Crumb previousCrumb = trace.next();
		  Crumb crumb = trace.next();
		  writeKMLHeader(crumb.getSLat(),crumb.getSLon(),"1000","44","-100");
		  
		   while(crumb != null)
		   {  
			   writeCrumb(crumb,previousCrumb);
			   previousCrumb = crumb;
			   crumb = trace.next();
		   }
		   writeKMLFooter();
		   close();
		  return(result);
	  }
	  
	  
	  

	
	  private void writeCrumb(Crumb c,Crumb previousCrumb)
	  {
				
		 
	  // FileErrorInfo fei = new FileErrorInfo();
	   SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
	   
	   
	    if (bFirstCrumb)  // change trace color
	    {
	    	
	    try
	    {
	    	bFirstCrumb = false;
	    	dos.writeBytes(
		 			       "<TimeStamp>\n"+
					       "  	<when>"+simpleDateFormat.format(c.getTime())+"</when>\n"+
					       "</TimeStamp>\n"+
	    				   "<Style>\n"+
	    			       "<LineStyle>\n"+
	    			       "  <color>"+colorBlender(Integer.toHexString(c.getColor().getRGB()))+"</color>\n"+
	                       "<width>"+c.getWidth()+"</width>"+
	    			       "</LineStyle>\n"+
	    			       "</Style>\n"+
	    			       "<LineString>\n"+
	    			       "<coordinates>\n");
	    }
	    catch(IOException ex)
	    {
	    	//eh.process(ErrorHandler.FILEWRITE_EXCEPTION,fei);
	    }
	    }

	    else /*if (c.getColor() != color)*/  // change trace color
	    {
	    try
	    {
	    	dos.writeBytes("</coordinates>\n"+
	    			       "</LineString>\n"+
	    			       "</Placemark>\n"+
	    			       "<Placemark><name>"+c.getSLat()+"</name>\n"+
	    			       "<TimeStamp>\n"+
	    			       "  	<when>"+simpleDateFormat.format(c.getTime())+"</when>\n"+
	    			       "</TimeStamp>\n"+
	    			       "<Style>\n"+
	    			       "<LineStyle>\n"+
	    			       "  <color>"+colorBlender(Integer.toHexString((color=c.getColor()).getRGB()))+"</color>\n"+
	    		           "<width>"+c.getWidth()+"</width>"+
	    			       "</LineStyle>\n");

	    	if (icrumbCounter > CRUMBSPERLOOK)
	    	{
	    		dos.writeBytes(
	    				"<LookAt>\n"+
	    					"<longitude>"+c.getSLon()+"</longitude>"+
	    					"<latitude>"+c.getSLat()+"</latitude>"+
	    					"<altitude>"+c.getSAlt()+"</altitude>"+
	    					"<range>1000</range>"+
	    					"<tilt>30</tilt>"+
	    					
	    				"</LookAt>\n");
	    				
	    						
	    	}
	    				  
	    	dos.writeBytes("</Style>\n"+
	    			       "<LineString>\n"+   			       
	    			       "<coordinates>\n");
	    	
	    	/* Write the last crumb again so there isn't a gap between lines */
	    	if (!c.comparePosition(previousCrumb))
	    		dos.writeBytes("     "+previousCrumb.getSLon()+","+previousCrumb.getSLat()+","+previousCrumb.getSAlt()+"\n");
	    }
	    catch(IOException ex)
	    {
	    	//eh.process(ErrorHandler.FILEWRITE_EXCEPTION,fei);
	    }
	    }
		  try
		  {
			  dos.writeBytes("     "+c.getSLon()+","+c.getSLat()+","+c.getSAlt()+"\n");
		  }
		  catch (IOException ex)
		  {
			//  eh.process(ErrorHandler.FILEWRITE_EXCEPTION,fei);
		  }
		  ++icrumbCounter;
	  }
}


